//
//  CFHomeVC.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/4/17.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFHomeVC.h"
#import "CFLiveCell.h"
#import "CFVarietiesCell.h"
#import "HomeHeaderView.h"
#import "CFHomeNoticeCell.h"
#import "SDCycleScrollView.h"
#import "CFHomeNoticeCell.h"
#import "CFRechargeViewController.h"
#import "CFUserinfoVC.h"

@interface CFHomeVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

@property (nonatomic,strong)SDCycleScrollView * cycleBannerView;

@property (nonatomic,strong)SDCycleScrollView *cycleNoticeView;

@end

@implementation CFHomeVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
     [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
   
    if (@available(iOS 11.0, *)) {//解决隐藏导航栏scrollView不能到顶的问题
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
        self.title = @"期达人";
    self.view.backgroundColor = DEF_COLOR_HOMEBGCOLOR;
    self.tableView.tableHeaderView.height = 407.5*Bit+30;
    [self SetUpNavView];
    [self creatBanner];
    
}

- (void)creatBanner{
    NSArray *imagesURLStrings = @[@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1524625393&di=eab0199e5411fe4721ff3825e44016c1&src=http://pic.90sjimg.com/back_pic/qk/back_origin_pic/00/04/01/a8e8afe94d0e1e912643537ad60dc540.jpg",
                                  @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524635489135&di=75cd8893b4b628a70999ae51e0c940c5&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F151026%2F14-151026104155V0.jpg",
                                  @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524635489135&di=4abfcf37a56364be282675787258f49c&imgtype=0&src=http%3A%2F%2Fpic.90sjimg.com%2Fback_pic%2Fqk%2Fback_origin_pic%2F00%2F04%2F01%2Fa8e8afe94d0e1e912643537ad60dc540.jpg",
                                  @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524635489135&di=c77d603b30cc3ba5333618916dc40245&imgtype=0&src=http%3A%2F%2Fimg3.3lian.com%2F2013%2Fc2%2F75%2Fd%2F21.jpg"
                                  ];
    self.cycleBannerView.imageURLStringsGroup = imagesURLStrings;
    self.cycleNoticeView.imageURLStringsGroup = imagesURLStrings;
}

- (void)SetUpNavView{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBtnItemWithNmlImg:@"home_set" hltImg:nil target:self action:@selector(setUp)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barBtnItemWithTitle:@"反馈" target:self action:@selector(feedback)];
}
- (void)setUp{
    CFUserinfoVC * userInfoVC = (CFUserinfoVC *)[CFUtility getVC:@"CFUserinfoVC" Storyboard:MINE];
    [self.navigationController pushViewController:userInfoVC animated:YES];
}

- (void)feedback{
    
}

#pragma mark 顶部按钮点击

- (IBAction)topImgClicked:(UITapGestureRecognizer *)sender {
    NSLog(@"点击了第%li个",sender.view.tag-10);

}

#pragma mark tableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 270;
    }
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 46.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section ==1){
        return 2;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HomeHeaderView * header = [[HomeHeaderView alloc] init];
    switch (section) {
        case 0:
            header.title.text = @"推荐品种";
            header.icon.image = [UIImage imageNamed:@"home_hot"];
            break;
        case 1:
            header.title.text = @"直播解盘";
            header.icon.image = [UIImage imageNamed:@"home_zhibo"];
            break;
            
        default:
            break;
    }
    header.clickedBlock = ^{
        NSLog(@"点击了头");
    };
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = DEF_COLOR_HOMEBGCOLOR;
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString * cellId = @"CFVarietiesCell";
        CFVarietiesCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        cell.selectedBlock = ^(NSInteger index) {
            NSLog(@"点击了%li",(long)index);
        };
        return cell;
    }else if (indexPath.section ==1){
        static NSString * cellId = @"CFLiveCell";
        CFLiveCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        NSLog(@"点直播了。");
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY>DEF_NAV_HEIGHT()) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }else{
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

- (SDCycleScrollView *)cycleBannerView{
    if (!_cycleBannerView) {
        _cycleBannerView = [SDCycleScrollView cycleScrollViewWithFrame:self.bannerView.bounds delegate:self placeholderImage:nil];
        _cycleBannerView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _cycleBannerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleBannerView.pageControlDotSize = CGSizeMake(7, 7);
        [self.bannerView addSubview:_cycleBannerView];
        [_cycleBannerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _cycleBannerView;
}

- (SDCycleScrollView *)cycleNoticeView{
    if (!_cycleNoticeView) {
        _cycleNoticeView = [SDCycleScrollView cycleScrollViewWithFrame:self.noticeView.bounds delegate:self placeholderImage:nil];
        _cycleNoticeView.backgroundColor = [UIColor clearColor];
        _cycleNoticeView.showPageControl = NO;
        _cycleNoticeView.scrollDirection = UICollectionViewScrollDirectionVertical;
        [_cycleNoticeView disableScrollGesture];
        [self.noticeView addSubview:_cycleNoticeView];
        [_cycleNoticeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _cycleNoticeView;
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"---点击了第%ld张图片", (long)index);
}

- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view
{
    if (view != _cycleNoticeView) {
        return nil;
    }
    
    
    return [CFHomeNoticeCell class];
}

- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view{
    CFHomeNoticeCell *myCell = (CFHomeNoticeCell *)cell;
    
}


@end
